package de.tum.in.far.threedui.general;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.media.j3d.Switch;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Cylinder;

public class ButtonGroup extends BranchGroup {

	public class ButtonController extends TimerTask implements
			ActionListener {

		boolean pressed, eventFired;

		private ButtonController() {
			this.pressed = false;
			this.eventFired = false;
			Timer timer = new Timer();
			timer.schedule(this, 2000, 250);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// if(pressed)
			eventFired = false;

			pressed = false;

		}

		@Override
		public void run() {
			// this.
			// System.out.println("timer fired !");
			if (!pressed) {
				pressed = true;

				if (ButtonGroup.this.buttonSwitch.getWhichChild() != 0) {
					ButtonGroup.this.buttonSwitch.setWhichChild(0);
				}
			} else {
				if (!this.eventFired) {
					ButtonGroup.this.buttonSwitch.setWhichChild(1);
					this.eventFired = true;
					buttonPressedEvent();
				}

			}

		}

		public void buttonPressedEvent()
		{
			for(ActionListener al: ButtonGroup.this.listeners)
			{
				al.actionPerformed(null);
			}
		}
	}

	TransformGroup transformGroup;
	Switch buttonSwitch;
	Vector<ActionListener> listeners;
	ButtonController controller;
	
	public ButtonGroup() {
		float nonPressedHeight = 0.023f;
		Appearance nonPressedAppearance = new Appearance();
		Material nonPressedMaterial = new Material();
		nonPressedMaterial.setAmbientColor(new Color3f(.5f, 0.5f, .8f));
		nonPressedMaterial.setDiffuseColor(new Color3f(.5f, .5f, 1f));
		nonPressedAppearance.setMaterial(nonPressedMaterial);

		Cylinder nonPressedCylinder = new Cylinder(0.023f, nonPressedHeight,
				nonPressedAppearance);
		Transform3D nonPressedCylinderRotationT3D = new Transform3D();
		nonPressedCylinderRotationT3D.rotX(Math.PI / 2);
		Transform3D nonPressedCylinderTranslationT3D = new Transform3D();
		nonPressedCylinderTranslationT3D.setTranslation(new Vector3f(0, 0,
				nonPressedHeight / 2));
		nonPressedCylinderTranslationT3D.mul(nonPressedCylinderRotationT3D);
		TransformGroup nonPressedTG = new TransformGroup(
				nonPressedCylinderTranslationT3D);
		nonPressedTG.addChild(nonPressedCylinder);

		float pressedHeight = 0.01f;
		Appearance pressedAppearance = new Appearance();
		Material pressedMaterial = new Material();
		pressedMaterial.setAmbientColor(new Color3f(.2f, 1f, .5f));
		pressedMaterial.setDiffuseColor(new Color3f(.5f, 1, .5f));
		pressedAppearance.setMaterial(pressedMaterial);

		Cylinder pressedCylinder = new Cylinder(0.023f, pressedHeight,
				pressedAppearance);
		Transform3D pressedCylinderRotationT3D = new Transform3D();
		pressedCylinderRotationT3D.rotX(Math.PI / 2);
		Transform3D pressedCylinderTranslationT3D = new Transform3D();
		pressedCylinderTranslationT3D.setTranslation(new Vector3f(0, 0,
				nonPressedHeight / 2));
		pressedCylinderTranslationT3D.mul(pressedCylinderRotationT3D);
		TransformGroup pressedTG = new TransformGroup(
				pressedCylinderTranslationT3D);
		pressedTG.addChild(pressedCylinder);

		buttonSwitch = new Switch();
		buttonSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
		buttonSwitch.addChild(nonPressedTG);
		buttonSwitch.addChild(pressedTG);
		buttonSwitch.setWhichChild(0);

		transformGroup = new TransformGroup();
		transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		transformGroup.addChild(buttonSwitch);
		addChild(transformGroup);
		
		this.controller = new ButtonController();
		this.listeners = new Vector<ActionListener>();

	}
	
	public void registerListener(ActionListener e)
	{
		this.listeners.add(e);
	}
	
	
	public ButtonController getButtonController()
	{
		return this.controller;
	}
	
}
